﻿using Furion.ViewEngine;
using Jint;
using RazorEngine.Templating;

namespace apitest.Application.Test.Views
{
    [DynamicApiController]
    public class ViewEngineService : IDynamicApiController
    {
        private readonly IViewEngine _viewEngine;
        public ViewEngineService(IViewEngine viewEngine)
        {
            _viewEngine = viewEngine;
            // var result = _viewEngine.RunCompile("Hello @Model.Name", new { Name = "Furion" });
            var result = _viewEngine.RunCompileFromCached(@"
<area>
    @{ RecursionTest(3); }
</area>

@{
  void RecursionTest(int level)
  {
	if (level <= 0)
	{
		return;
	}

	<div>LEVEL: @level</div>
	@{ RecursionTest(level - 1); }
  }
}
");
        }

        public async Task<dynamic> T1()
        {
            return _viewEngine.RunCompileFromCached(@"
<area>
    @{ RecursionTest(3); }
</area>

@{
  void RecursionTest(int level)
  {
	if (level <= 0)
	{
		return;
	}

	<div>LEVEL: @level</div>
	@{ RecursionTest(level - 1); }
  }
}
");
        }

        [NonUnify,HttpGet]
        public async Task<dynamic> T2()
        {
            var path = "./test.cshtml";
            //打开并且读取模板
            string template = File.ReadAllText(path); 
          //  string template = "Hello @Model.Name, welcome to RazorEngine!";

            return   _viewEngine.RunCompile(template,new Student()
          {
              Name="张三",
              Age=123
          });
        }

        public class Student
        {
            public string Name { get; set; }
            public int Age { get; set; }
        }
    }

}
